package com.dfdchem.imd.kaiwu.activity.base;

import android.content.Context;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Interceptor;
import com.alibaba.android.arouter.facade.callback.InterceptorCallback;
import com.alibaba.android.arouter.facade.template.IInterceptor;
import com.alibaba.android.arouter.launcher.ARouter;
import com.dfdchem.imd.kaiwu.constant.RouterPath;
import com.dfdchem.imd.kaiwu.manager.DataManager;

import zuo.biao.library.util.StringUtil;

@Interceptor(priority = 99, name = "登录授权拦截器")
public class AuthInterceptor implements IInterceptor {
    private static final String TAG = "AuthInterceptor";
    private Context context;

    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {

        String path = postcard.getPath();
        String xToken = DataManager.getInstance().getXToken();

        if (StringUtil.isEmpty(xToken)) {
            switch (path) {
                // 不需要登录的页面
                case RouterPath.SPLASH_ACTIVITY:
                case RouterPath.SPLASH_AD_ACTIVITY:
                case RouterPath.LOGIN_ACTIVITY:
                case RouterPath.REGISTER_ACTIVITY:
                case RouterPath.GUIDE_ACTIVITY:
                case RouterPath.ENV_SETTING_ACTIVITY:
                    callback.onContinue(postcard);
                    break;
                default:
                    ARouter.getInstance().build(RouterPath.LOGIN_ACTIVITY).navigation(context);
                    break;
            }
        } else {
            callback.onContinue(postcard);
        }
    }

    @Override
    public void init(Context context) {
        this.context = context;
    }
}
